package com.cdshop.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.cdshop.comm.ApplicationException;
import com.cdshop.entity.Goods;
import com.cdshop.jdbc.ResultSetCB;

public final class GoodsDAO extends AbstractDAO<Goods> {

	protected GoodsDAO() {
	}

	protected static GoodsDAO singleton = new GoodsDAO();

	public static GoodsDAO instance() {
		return singleton;
	}

	static final String List_Query = "SELECT * FROM Goods ORDER BY tstamp DESC";
	public static final ResultSetCB<List<Goods>> Goods_RSCB = new ResultSetCB<List<Goods>>() {

		@Override
		public List<Goods> call(ResultSet rs, int count) throws SQLException {
			List<Goods> list = new ArrayList<Goods>();
			while (rs.next()) {
				Goods goods = new Goods();
				goods.setId(rs.getLong("id"));
				goods.setName(rs.getString("name"));
				goods.setDesc(rs.getString("desc"));
				goods.setPrice(rs.getDouble("price"));
				goods.setTstamp(rs.getDate("tstamp"));
				goods.setCategory(rs.getShort("category"));
				goods.setBarcode(rs.getString("barcode"));
				list.add(goods);
			}
			return list;
		}
	};

	@Override
	public Goods create(Goods entity) throws ApplicationException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Goods retrive(long id) throws ApplicationException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Goods update(Goods entity) throws ApplicationException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Goods delete(Goods entity) throws ApplicationException {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Goods> list() throws ApplicationException {
		try {
			return dbService.executeQuery(List_Query, Goods_RSCB);
		} catch (SQLException e) {
			throw new ApplicationException(e);
		}
	}

}
